package com.example.hasee.zhcd.Activities.ShangWu;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Activities.ChuanWu.AtWorkActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.LPEntrustActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.ShipDetailActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.WayBillActivity;
import com.example.hasee.zhcd.Activities.QianDao.TagActivity;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.AtPortItemInfo;
import com.example.hasee.zhcd.Bean.BrandItemInfo;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import static anet.channel.util.Utils.context;
import static com.example.hasee.zhcd.Activities.ChuanWu.OBInputActivity.brandInfoList;
import static com.example.hasee.zhcd.Application.App.map;

/**
 * Created by hasee on 2018/6/1.
 */

public class AtPortActivity extends BaseActivity {
    private HashMap<String, String> loginmap = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private String strRoleID;
    private String strSYN;
    private String strAccount;
    private TextView title;
    private ImageView back;
    private ImageView home;
    private ListView at_port_list;
    public static ArrayList<AtPortItemInfo> list = new ArrayList<AtPortItemInfo>();
    private AtPortAdapter adapter;
    private ArrayList<Boolean> bol_list = new ArrayList<Boolean>();
    private CheckBox my_ship;
    private CheckBox no_ship;
    private CheckBox point_ship;
    private String strME = "0";
    private String strWZG = "0";
    private String strZD = "0";

    @Override
    public int contentViewId() {
        return R.layout.activity_at_port;
    }

    @Override
    public void set() {
        title.setText("在港船舶");
        back.setOnClickListener(this);
        home.setOnClickListener(this);
        home.setImageResource(R.mipmap.edit);
        my_ship.setChecked(false);
        my_ship.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    strME = "1";
                } else {
                    strME = "0";
                }
                getAtPortList();
            }
        });
        no_ship.setChecked(false);
        no_ship.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    strWZG = "1";
                } else {
                    strWZG = "0";
                }
                getAtPortList();
            }
        });
        point_ship.setChecked(false);
        point_ship.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    strZD = "1";
                } else {
                    strZD = "0";
                }
                getAtPortList();
            }
        });
        getAtPortList();
    }

    @Override
    public void init() {
        strRoleID = loginmap.get("role");
        strAccount = loginmap.get("account");
        strSYN = "";
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        at_port_list = (ListView) findViewById(R.id.at_port_list);
        my_ship = (CheckBox) findViewById(R.id.my_ship);
        no_ship = (CheckBox) findViewById(R.id.no_ship);
        point_ship = (CheckBox) findViewById(R.id.point_ship);

    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "ZHSW_zgcbList":
                ProgressDialogUtils.closeProgressDialog();
                // Log.e("ZHSW_zgcbList", msg.getMsg());
                //{"fsyn":"QHD201802454", "fshipcode":"15058", "fshipname":"长昌南海", "fhc":"1803", "fzygs":"",
                // "fyddate":"06-07 15:00", "fberth":"", "fbyj":"", "fbyjlr":"备用金录入", "fsfyg":"50850.00",
                // "fsfyglr":"使费预估录入", "fstatus":"预抵", "fwxp":"", "fwbsx":""}
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        list.clear();
                        for (int i = 0; i < data.length(); i++) {
                            AtPortItemInfo info = new AtPortItemInfo();
                            info.setFsyn(data.getJSONObject(i).getString("fsyn"));
                            info.setFshipcode(data.getJSONObject(i).getString("fshipcode"));
                            info.setFshipname(data.getJSONObject(i).getString("fshipname"));
                            info.setFhc(data.getJSONObject(i).getString("fhc"));
                            info.setFzygs(data.getJSONObject(i).getString("fzygs"));
                            info.setFyddate(data.getJSONObject(i).getString("fyddate"));
                            info.setFberth(data.getJSONObject(i).getString("fberth"));
                            info.setFbyj(data.getJSONObject(i).getString("fbyj"));
                            info.setFbyjlr(data.getJSONObject(i).getString("fbyjlr"));
                            info.setFsfyg(data.getJSONObject(i).getString("fsfyg"));
                            info.setFsfyglr(data.getJSONObject(i).getString("fsfyglr"));
                            info.setFstatus(data.getJSONObject(i).getString("fstatus"));
                            info.setFwbsx(data.getJSONObject(i).getString("fwbsx"));
                            info.setSelected(false);
                            list.add(info);
                        }
                        adapter = new AtPortAdapter();
                        at_port_list.setAdapter(adapter);
                        at_port_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                            }
                        });
                    } else {
                        Toast.makeText(AtPortActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "ZHSW_zgcb_saveWXP":
                ProgressDialogUtils.closeProgressDialog();
//                Log.e("ZHSW_zgcb_saveWXP", msg.getMsg());
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    Toast.makeText(AtPortActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                getAtPortList();
                break;
        }
    }

    private void getAtPortList() {
        ProgressDialogUtils.createProgressDialog(AtPortActivity.this, "正在获取在港船列表...");
        HashMap<String, String> map = new HashMap<>();
        map.put("strRoleID", strRoleID);
        map.put("strME", strME);
        map.put("strWZG", strWZG);
        map.put("strZD", strZD);

        httpUtils.get("ZHSW_zgcbList", map, "ZHSW_zgcbList");
    }

    private void addWXP() {
        ProgressDialogUtils.createProgressDialog(AtPortActivity.this, "正在提交危险品申请...");
        HashMap<String, String> map = new HashMap<>();
        map.put("strSYN", strSYN);
        map.put("strAccount", strAccount);
        httpUtils.get("ZHSW_zgcb_saveWXP", map, "ZHSW_zgcb_saveWXP");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.home:
                strSYN = "";
                bol_list.clear();
                for (int j = 0; j < list.size(); j++) {
                    bol_list.add(list.get(j).isSelected());
                }
                if (bol_list.contains(true)) {
                    for (int k = 0; k < bol_list.size(); k++) {
                        if (bol_list.get(k) == true) {
                            if ("".equals(strSYN)) {
                                strSYN = list.get(k).getFsyn();
                            } else {
                                strSYN = strSYN + "," + list.get(k).getFsyn();
                            }
                        }
                    }
                    Log.e("strSYN", strSYN);
                    addWXP();
                } else {
                    Toast.makeText(AtPortActivity.this, "请选择需要申请危险品的船舶！", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    class AtPortAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null != list ? list.get(position) : null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            AtPortItemInfo info = (AtPortItemInfo) getItem(position);
            if (null == holder) {
                LayoutInflater inflater = (LayoutInflater) AtPortActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.at_port_list_item, null);
                holder = new Holder();
                holder.detail = (TextView) convertView.findViewById(R.id.detail);
                holder.linear = (LinearLayout) convertView.findViewById(R.id.linear);
                holder.line = (View) convertView.findViewById(R.id.line);
                holder.fstatus = (TextView) convertView.findViewById(R.id.fstatus);
                holder.fshipname = (TextView) convertView.findViewById(R.id.fshipname);
                holder.fshipname.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(AtPortActivity.this, ShipDetailActivity.class);
                        intent.putExtra("strSYN", list.get(position).getFsyn());
                        startActivity(intent);
                    }
                });
                holder.fhc = (TextView) convertView.findViewById(R.id.fhc);
                holder.fzygs = (TextView) convertView.findViewById(R.id.fzygs);
                holder.fyddate = (TextView) convertView.findViewById(R.id.fyddate);
                holder.fberth = (TextView) convertView.findViewById(R.id.fberth);
                holder.fbyj = (TextView) convertView.findViewById(R.id.fbyj);
                holder.fbyjlr = (TextView) convertView.findViewById(R.id.fbyjlr);
                holder.fsfyg = (TextView) convertView.findViewById(R.id.fsfyg);
                holder.fsfyglr = (TextView) convertView.findViewById(R.id.fsfyglr);
                holder.fwxp = (CheckBox) convertView.findViewById(R.id.fwxp);
                holder.fwbsx = (TextView) convertView.findViewById(R.id.fwbsx);
                holder.border=(LinearLayout)convertView.findViewById(R.id.border);
                final Holder fholder = holder;
                holder.fwxp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        AtPortItemInfo info = (AtPortItemInfo) fholder.fwxp.getTag();
                        info.setSelected(buttonView.isChecked());
                    }
                });
                convertView.setTag(holder);
                holder.fwxp.setTag(info);
            } else {
                holder = (Holder) convertView.getTag();
                holder.fwxp.setTag(info);
            }
            holder.fstatus.setText((String) info.getFstatus());
            holder.fshipname.setText((String) info.getFshipname());
            holder.fshipname.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            holder.fhc.setText((String) info.getFhc());
            if (info.getFzygs().length() == 0) {
                holder.fzygs.setText((String) info.getFzygs());
            } else {
                holder.fzygs.setText((String) info.getFzygs());
                holder.fzygs.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                holder.fzygs.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(AtPortActivity.this, WayBillActivity.class);
                        intent.putExtra("strSYN", list.get(position).getFsyn());
                        startActivity(intent);
                    }
                });
            }
            holder.fyddate.setText((String) info.getFyddate());
            holder.fberth.setText((String) info.getFberth());
            holder.fbyj.setText((String) info.getFbyj());
            if(list.get(position).getFsyn().length()==0){
                holder.border.setBackgroundResource(R.drawable.border_item_yellow);
                holder.linear.setVisibility(View.GONE);
                holder.line.setVisibility(View.GONE);
            }else {
                holder.border.setBackgroundResource(R.drawable.border_item);
                holder.linear.setVisibility(View.VISIBLE);
                holder.line.setVisibility(View.VISIBLE);
                holder.detail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(AtPortActivity.this, AtPortDetailActivity.class);
                        intent.putExtra("strSYN", list.get(position).getFsyn());
                        startActivity(intent);
                    }
                });
            }
            holder.fwxp.setChecked(info.isSelected());
            if (info.getFbyjlr().length() == 0) {
                holder.fbyjlr.setVisibility(View.INVISIBLE);
            } else {
                holder.fbyjlr.setVisibility(View.VISIBLE);
                holder.fbyjlr.setText((String) info.getFbyjlr() + ">>");
                holder.fbyjlr.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(AtPortActivity.this, FundActivity.class);
                        intent.putExtra("strSYN", list.get(position).getFsyn());
                        intent.putExtra("strShip", list.get(position).getFshipcode());
                        startActivity(intent);
                    }
                });
            }

            if (info.getFsfyg().length() == 0) {
                holder.fsfyg.setVisibility(View.INVISIBLE);
            } else {
                holder.fsfyg.setVisibility(View.VISIBLE);
                holder.fsfyg.setText((String) info.getFsfyg());
                holder.fsfyg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(AtPortActivity.this, FMDetailActivity.class);
                        intent.putExtra("strSYN", list.get(position).getFsyn());
                        startActivity(intent);
                    }
                });
            }
            if (info.getFsfyglr().length() == 0) {
                holder.fsfyglr.setVisibility(View.INVISIBLE);
            } else {
                holder.fsfyglr.setVisibility(View.VISIBLE);
                holder.fsfyglr.setText((String) info.getFsfyglr() + ">>");
                holder.fsfyglr.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(AtPortActivity.this, FMInputActivity.class);
                        intent.putExtra("strSYN", list.get(position).getFsyn());
                        startActivity(intent);
                    }
                });
            }
            if (info.getFwbsx().length() == 0) {
                holder.fwbsx.setVisibility(View.INVISIBLE);
            } else {
                holder.fwbsx.setVisibility(View.VISIBLE);
//                holder.fsfyglr.setText((String) info.getFsfyglr() + ">>");
                holder.fwbsx.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(AtPortActivity.this, LPEntrustActivity.class);
                        intent.putExtra("fshipcode", list.get(position).getFshipcode());
                        intent.putExtra("fshipyearnumvcr", list.get(position).getFsyn());
                        startActivity(intent);
                    }
                });
            }
            return convertView;
        }

        class Holder {
            private TextView fstatus;
            private TextView fshipname;
            private TextView fhc;
            private TextView fzygs;
            private TextView fyddate;
            private TextView fberth;
            private TextView fbyj;
            private TextView fbyjlr;
            private TextView fsfyg;
            private TextView fsfyglr;
            private CheckBox fwxp;
            private LinearLayout linear;
            private View line;
            private TextView fwbsx;
            private TextView detail;
            private LinearLayout border;
        }
    }
}
